Ext.ux.ModifyNews = Ext.extend(Ext.Window, {
	initComponent : function(config) {
		var basePath = document.getElementById('basePath').href;
		var linedata = Ext.getCmp("newsDataGrid").getStore()
				.getAt(Ext.getCmp("newsDataGrid").getSelectionModel().lastActive);
		var lang;
		
		var langStore = new Ext.data.ArrayStore({
	        fields: ['lang', 'value'],
	        data : [['简体中文', 'zh_CN'],['繁体中文', 'zh_TW'],['美式英文', 'en_US']]
	    });
		
		var category = new Ext.data.Store( {
			proxy: new Ext.data.HttpProxy({url: basePath + 'base/list.action?whoCategory=news'}),
			reader:new Ext.data.ArrayReader({idIndex: 0},
				Ext.data.Record.create([{name: 'id'}, {name: 'name'}]))
		});
		
		var modifyForm = new Ext.FormPanel({
        	labelWidth : 100, // label settings here cascade unless overridden
			url : basePath + 'news/update.action',
			autoScroll:true,
			frame : true,
			region : 'center',
			border:true,
			fileUpload: true,
			bodyStyle : 'padding:5px 5px 0',
			width : '100%',
			defaults : {
				width : 220
			},
			defaultType : 'textfield',

			items : [{
				xtype : "combo",
				fieldLabel : '选择语言',
				store : langStore,
				id:'newsLangUpdate',
				mode : 'local',
				valueField: 'value',
				displayField : 'lang',
				allowBlank : false,
				typeAhead : true,
				triggerAction : 'all',
				editable:false,
				forceSelection : true,
				selectOnFocus : true,
				listeners:{
					select : function(combo, record, index) {
						category.removeAll();
						lang = record.get('value');
						category.proxy.setUrl(basePath + 'base/list.action?whoCategory=news&lang=' + lang);
						category.load();
					}
				}
			}, {
				fieldLabel : '新闻标题',
				name : 'news.title',
				id:'newsTitleUpdate',
				allowBlank : false
			}, {
				fieldLabel : '新闻摘要',
				name : 'news.abstracts',
				id:'newsAbstractsUpdate',
				allowBlank : false
			}, {
				xtype : "combo",
				fieldLabel : '新闻类别',
				store : category,
				displayField : 'name',
				typeAhead : true,
				valueField: 'id',
				id:'newsCategoryUpdate',
				name:'category',
				mode : 'local',
				triggerAction : 'all',
				editable:false,
				forceSelection : true,
				selectOnFocus : true
			}, {
				xtype:'StarHtmleditor',
				fieldLabel : '新闻内容',
				anchor:'95%',
				height:300,
				allowBlank : false,
				autoScroll:true,
				url:basePath + 'base/upload.action',
				id:'newsContentUpdate',
				name : 'news.content'
			}],
			bbar : [{
				xtype:'spacer',
				width:50
			},{
				text:'添加图片',
				tooltip:'添加多张新闻图片',
				tooltipType:'qtip',
				handler:function() {
					modifyForm.add({
						fieldLabel : '产品图片',
						name:'newsImage',
						inputType : 'file',
						allowBlank : false,
						editable:false
					});
					modifyForm.doLayout();
				}
			},'->', {
				text : '保存',
				handler:function() {
					if(!modifyForm.form.isValid()) {
						return;
					}
					modifyForm.form.submit( {
						waitMsg : '正在上传......',
						url : basePath + 'news/update.action?news.id=' + linedata.json.id + 
							'&news.lang=' + lang,
						method : "POST",
						success : function(form, action) {
							form.reset();
							Ext.getCmp('modifyNewsWin').close();
							Ext.MessageBox.alert('提示', '更新成功');
							Ext.StoreMgr.get('newsStore').reload();
						},
						failure : function(form, action) {
							if (action.failureType == Ext.form.Action.SERVER_INVALID)
								Ext.MessageBox.alert('警告', action.result.msg);
						}
					});
				}
			}, {
				xtype:'spacer',
				width:50
			}]
		});
		
		Ext.Ajax.request( {
			url : basePath + 'news/load.action?news.id=' + linedata.json.id,
			method : 'post',
			callback : function(op, success, response) {
				var jsonObject = eval('(' + response.responseText + ')');
				Ext.getCmp("newsTitleUpdate").setValue(jsonObject.title);
    			Ext.getCmp("newsAbstractsUpdate").setValue(jsonObject.abstracts);
    			Ext.getCmp("newsCategoryUpdate").setValue(jsonObject.category.name);
    			Ext.getCmp("newsContentUpdate").setValue(jsonObject.content);
    			var langName;
    			switch(jsonObject.lang) {
    				case 'zh_CN' : langName = '简体中文'; break;
    				case 'zh_TW' : langName = '繁体中文'; break;
    				case 'en_US' : langName = '美式英文'; break;
    			}
    			Ext.getCmp("newsLangUpdate").setValue(langName);
    			lang = jsonObject.lang;
			}
		});
		
	    var imagesPanel = new Ext.Panel({
	        title:'新闻图片',
	        region:'east',
	        layout:'vbox',
			split : true,
			frame : true,
			maxWidth : 210,
			minWidth : 210,
			width:210,
			autoScroll:true,
			collapsible : true,
			collapseMode: 'mini',
	        items: new Ext.DataView({
		        itemSelector: 'div.thumb-wrap',
		        style:'overflow:auto',
		        multiSelect: true,
		        width:'100%',
		        height:'100%',
		        store: new Ext.data.JsonStore({
				    url: basePath + 'news/images.action?news.id=' + linedata.json.id,
				    root: 'images',
			        storeId:'newsImagesStore',
				    autoLoad:true,
				    fields: ['name', 'path', 'id']
				}),
		        tpl: new Ext.XTemplate(
		            '<tpl for=".">',
		            '<div class="thumb-wrap" id="{name}">',
		            '<div class="thumb"><img src="' + basePath + '{path}"' +
		            ' width=180><br/>' +
		            '<a href=# onclick=ajaxDeleteImage("{id},news,delete")>' +
		            '彻底删除</a>&nbsp;&nbsp;&nbsp;' +
		            '<a href=# onclick=ajaxDeleteImage("{id},news,update")>' +
		            '可恢复删除</a>',
		            '</div></div>',
		            '</tpl>'
		        )
		    })
	    });
		
		Ext.apply(this, {
			title: "修改新闻信息",
	        plain: true,
	        width:1000,
	        height:600,
	        modal: true,
	        maximizable:true,
	        border: false,
	        layout: 'border',
	        autoScroll:true,
	        items: [modifyForm, imagesPanel]
		});
		Ext.ux.ModifyNews.superclass.initComponent.apply(this, arguments);
	}
});
Ext.reg('lgnModifyNews', Ext.ux.ModifyNews);
